package com.stylefeng.guns.modular.small.controller;

import com.stylefeng.guns.config.redis.RedisUtil;
import com.stylefeng.guns.modular.small.service.IBizConsumerService;
import com.stylefeng.guns.modular.small.util.CheckPhoneUtil;
import com.stylefeng.guns.modular.small.util.ResponseCode;
import com.stylefeng.guns.modular.small.util.ResponseResultDTO;
import com.stylefeng.guns.modular.system.service.IArtificerMeansService;
import com.stylefeng.guns.modular.system.service.ILoginLogService;
import com.stylefeng.guns.modular.wehcat.api.WechatApi;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @apiDefine BizConsumerNotFoundError
 *
 * @apiError BizConsumerNotFound The id of the BizConsumer was not found.
 *
 * @apiErrorExample Error-Response:
 *     HTTP/1.1 404 Not Found
 *     {
 *       "error": "BizConsumerNotFound"
 *     }
 */
@Controller
@Slf4j
@RequestMapping("/wx/small")
public class WxLoginController {

    @Autowired
    private IBizConsumerService bizConsumerService;

    @Autowired
    private IArtificerMeansService iArtificerMeansService;

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private ILoginLogService loginLogService;

    @Resource
    private WechatApi wechatApi;
    /**
     * @api {POST} /wx/small/login  用户登录/技师登录
     * @apiVersion 1.1.0
     * @apiName login
     * @apiGroup  smallLogin
     *
     * @apiParam {String} phone 登录的手机.
     * @apiParam {String} code  登录的短信验证码.
     * @apiParam {String} userType  登录的用户类型 1为技师  0为用户.
     *
     * @apiSuccess {String}   msg        返回错误信息.
     * @apiSuccess {String}   status     返回错误状态  0000 为正确，-1为业务错误.
     * @apiSuccess {String}   userType   用户的类型 1为技师登录，0为用户登录.
     * @apiSuccess {Object}   data      返回登录用户的对象.
     *
     * @apiSuccessExample Success-Response:
     *     HTTP/1.1 200 OK
     *     {
     * 	    "data": {
     * 		        "companyId": "",
     * 		        "createTime": "2019-09-10 22:59:48",
     * 		        "distincts": "",
     * 		        "enabled": 1,
     * 		        "evaluation": "",
     * 		        "id": 3,
     * 		        "idcard": "",
     * 		        "imgUrl": "",
     * 		        "introduction": "",
     * 		        "latitude": "",
     * 		        "longitude": "",
     * 		        "name": "15982276786",
     * 		        "rank": "",
     * 		        "realName": "",
     * 		        "serviceArea": "",
     * 		       "sex": "",
     * 		       "stat": "",
     * 		       "tel": "15982276786",
     * 		       "updateTime": "",
     * 		       "viewProfessionalTitle": ""
     *          },
     * 	   "msg": "SUCCESS",
     * 	   "status": "0000",
     * 	  "userType": "1"
     * }
     *
     * @apiErrorExample Error-Response:
     *     HTTP/1.1 500 Not Found
     *     {
     * 	      "data": "",
     * 	      "msg": "短信验证码错误",
     *    	  "status": "-1",
     *   	  "userType": "1"
     *     }
     */
    @RequestMapping("/login")
    @ResponseBody
    public ResponseResultDTO wxLogin(String phone, String code, String userType,String  wxCode) {
        log.info(">>>>>>>>>>>>>>>>>>>>>>>>获取的微信的code是：【{}】", wxCode);
        String openId = null;
        if(StringUtils.isNotBlank(wxCode))
            openId=wechatApi.getOpenId(wxCode);
        log.info(">>>>>>>>>>>>>>>>>>>>>>>>获取登录的手机的openId是：【{}】", openId);
        log.info("登录的手机为:{}", phone);
        ResponseResultDTO responseResultDTO = new ResponseResultDTO();
        if (!CheckPhoneUtil.isPhone(phone)) {
            responseResultDTO.setStatus("-1");
            responseResultDTO.setMsg("手机号码不正确!");
            return responseResultDTO;
        }
        if (StringUtils.isBlank(code)) {
            responseResultDTO.setStatus("-1");
            responseResultDTO.setMsg("手机验证码不能为空!");
            return responseResultDTO;
        }
        try {
            if ("1".equals(userType))
                responseResultDTO = iArtificerMeansService.login(phone, code);
            else
                responseResultDTO = bizConsumerService.login(phone, code,openId);

        } catch (Exception e) {
            log.info("系统异常，异常信息：{}", e.getMessage());
            responseResultDTO.setMsg(ResponseCode.ERROR.getDesc());
            responseResultDTO.setStatus(ResponseCode.ERROR.getCode());
        }
        return responseResultDTO;
    }

    /**
     * @api {POST} /wx/small/getSmCode  获取短信验证码
     * @apiVersion 1.1.0
     * @apiName  getSmCode
     * @apiGroup  smallLogin
     *
     * @apiParam {String} phone 登录的手机.
     *
     * @apiSuccess {String}   msg        返回错误信息.
     * @apiSuccess {String}   status     返回错误状态  0000 为正确，-1为业务错误.
     * @apiSuccess {String}   [userType]   用户的类型 1为技师登录，0为用户登录.
     * @apiSuccess {Object}   data      返回的短信验证码.
     *
     * @apiSuccessExample Success-Response:
     *     HTTP/1.1 200 OK
     *      {
     * 	      "data": "4785",
     * 	      "msg": "SUCCESS",
     * 	      "status": "0000",
     * 	      "userType": ""
     *      }
     *
     */
    @RequestMapping("/getSmCode")
    @ResponseBody
    public ResponseResultDTO getSmCode(String phone) {
        log.info("发送短信的手机为:{}", phone);
        ResponseResultDTO responseResultDTO = new ResponseResultDTO();
        if (!CheckPhoneUtil.isPhone(phone)) {
            responseResultDTO.setStatus("-1");
            responseResultDTO.setMsg("手机号码不正确!");
            return responseResultDTO;
        }
        try {
            responseResultDTO = bizConsumerService.getSmCode(phone);
        } catch (Exception e) {
            log.info("系统异常，异常信息：{}", e.getMessage());
            responseResultDTO.setMsg(ResponseCode.ERROR.getDesc());
            responseResultDTO.setStatus(ResponseCode.ERROR.getCode());
        }
        return responseResultDTO;
    }

    /**
     * @api {POST} /wx/small/loginOut  退出登录.
     * @apiVersion 1.1.0
     * @apiName  loginOut
     * @apiGroup  smallLogin
     *
     * @apiParam {String} phone 登录的手机.
     *
     * @apiSuccess {String}   msg        返回错误信息.
     * @apiSuccess {String}   status     返回错误状态  0000 为正确，-1为业务错误.
     *
     * @apiSuccessExample Success-Response:
     *     HTTP/1.1 200 OK
     *      {
     * 	      "data": "",
     * 	      "msg": "SUCCESS",
     * 	      "status": "0000",
     * 	      "userType": ""
     *      }
     *
     */
    @RequestMapping("/loginOut")
    @ResponseBody
    public ResponseResultDTO loginOut(HttpServletRequest request, String phone) {
        log.info("发送短信的手机为:{}", phone);
        ResponseResultDTO responseResultDTO = new ResponseResultDTO();
        if (StringUtils.isBlank(phone)) {
            responseResultDTO.setStatus("-1");
            responseResultDTO.setMsg("PARAM-NULL");
            return responseResultDTO;
        }
        try {
            redisUtil.del(phone);
            //记录登录日志
        } catch (Exception e) {
            log.info("系统异常，异常信息：{}", e.getMessage());
            responseResultDTO.setMsg(ResponseCode.ERROR.getDesc());
            responseResultDTO.setStatus(ResponseCode.ERROR.getCode());
        }
        return responseResultDTO;
    }

}
